Skip to content

Don't ignore figsize in df.boxplot #16445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
May 24, 2017
Merged

Don't ignore figsize in df.boxplot #16445

merged 6 commits into from
May 24, 2017

Conversation

huguesv
Copy link
Contributor

@huguesv huguesv commented May 23, 2017

Work in progress from PyCon sprints

Fix for #11959

It really doesn't feel like a proper fix, but I'm not sure what a proper fix would be (without changing matplotlib). The matplotlib gca() ends up calling figure(), but doesn't let us pass in a figsize to gca().

Any advice? FYI this works, ie it fixes the behavior for the code snippet in the bug report. It would obviously break in multithreaded scenarios, but I'm not sure if that's a consideration or not.

@TomAugspurger TomAugspurger added the Visualization plotting label May 23, 2017
@TomAugspurger
Copy link
Contributor

Thanks @huguesv. I think the issue is somewhere around

ax = _gca()

This simplest fix would be to wrap that call in a plt.rc_context like

In [8]: with plt.rc_context({'figure.figsize': figsize}):
   ...:     ax = plt.gca()

Alternatively you could modify _gca to take an rc (a dictionary) like plt.rc_context. And just pass that through. Make sense?

@TomAugspurger
Copy link
Contributor

Sorry I got confused by the diff. I see that you found the right line :)

if ax is None and len(plt.get_fignums()) > 0:
ax = _gca()
ax = _gca(figsize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would maybe revert this change, since we pass figsize through to _plot, presumably it's handled there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I missed that, thanks. Changing it back.

@codecov
Copy link

codecov bot commented May 23, 2017

Codecov Report

Merging #16445 into master will decrease coverage by <.01%.
The diff coverage is 70%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #16445      +/-   ##
==========================================
- Coverage   90.42%   90.41%   -0.01%     
==========================================
  Files         161      161              
  Lines       51023    51029       +6     
==========================================
+ Hits        46138    46140       +2     
- Misses       4885     4889       +4
Flag Coverage Δ
#multiple 88.25% <70%> (-0.01%) ⬇️
#single 40.16% <10%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/plotting/_core.py 81.75% <70%> (-0.15%) ⬇️
pandas/core/common.py 91.05% <0%> (-0.34%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 49ec31b...3df3a2e. Read the comment docs.

@codecov
Copy link

codecov bot commented May 23, 2017

Codecov Report

Merging #16445 into master will increase coverage by <.01%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #16445      +/-   ##
==========================================
+ Coverage   90.42%   90.42%   +<.01%     
==========================================
  Files         161      161              
  Lines       51023    51027       +4     
==========================================
+ Hits        46138    46142       +4     
  Misses       4885     4885
Flag Coverage Δ
#multiple 88.26% <100%> (ø) ⬆️
#single 40.17% <20%> (-0.01%) ⬇️
Impacted Files Coverage Δ
pandas/plotting/_core.py 81.92% <100%> (+0.02%) ⬆️
pandas/io/formats/style.py 95.92% <0%> (+0.01%) ⬆️
pandas/io/excel.py 62.31% <0%> (+0.05%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a8a497f...bab6ea2. Read the comment docs.

@TomAugspurger TomAugspurger modified the milestones: 0.21.0, 0.20.2 May 23, 2017
@TomAugspurger
Copy link
Contributor

You can add a release note in doc/source/whatsnew/v0.20.2.txt under the "Other Enhancements" section, about how DataFrame.boxplot now respects the figsize keyword for non-grouped boxplots.

@huguesv
Copy link
Contributor Author

huguesv commented May 23, 2017

Oops, changed the wrong whatsnew file, let me fix that.

@@ -49,9 +49,13 @@ def _get_standard_kind(kind):
return {'density': 'kde'}.get(kind, kind)


def _gca():
def _gca(figsize=None):
Copy link
Contributor

@TomAugspurger TomAugspurger May 23, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change _gca to take an optional dictionary? So like

def _gca(rc=None):
    with plt.rc_context(rc):
        return plt.gca()

that way, we aren't limited to just figsize when we call gca. You'd call it like ax = _gca({'figure.figsize': figsize})

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do that. Note that I can't unconditionally overwrite figure.figsize, because if it's not specified, it's None, and that would overwrite the default in rcParams. That's why I have it pass in an empty dict when it's None. Anyway, I can push that one level up the call stack like you mention.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah that's unfortunate. Still probably best to handle it outside.

@TomAugspurger
Copy link
Contributor

Did you have a test earlier? I thought you did, but maybe not. Anyway, could you add one in pandas/tests/plotting/test_boxplot_method.py Just need to verify that ax = df.boxplot(figsize=(4, 4)) or whatver actually has the correct figsize.

@huguesv
Copy link
Contributor Author

huguesv commented May 23, 2017

Added test.

@slow
def test_figsize(self):
df = DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
result = df.boxplot(return_type='axes', figsize=(12,8))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll need a comma between the 12 and 8, otherwise our style cheker will complain.

@@ -160,6 +160,13 @@ def test_boxplot_empty_column(self):
df.loc[:, 0] = np.nan
_check_plot_works(df.boxplot, return_type='axes')

@slow
def test_figsize(self):
df = DataFrame(np.random.rand(10, 5), columns=['A', 'B', 'C', 'D', 'E'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this line might be a bit longn too. flake8 pandas/tests/plotting/test_boxplot_method.py should tell you.

Copy link
Contributor

@TomAugspurger TomAugspurger left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great. Let me know if you notice that all the tests pass before I do.

@TomAugspurger
Copy link
Contributor

The one test failure seemed unrealted. I've restarted it to be safe though: https://travis-ci.org/pandas-dev/pandas/jobs/235323266

@huguesv
Copy link
Contributor Author

huguesv commented May 23, 2017

All tests passed! :)

@TomAugspurger TomAugspurger merged commit 044feb5 into pandas-dev:master May 24, 2017
@TomAugspurger
Copy link
Contributor

@huguesv thanks. Nice job!

TomAugspurger pushed a commit to TomAugspurger/pandas that referenced this pull request May 29, 2017
* Propagate the figsize via the rcParams, since matplotlib doesn't allow passing it as a parameter to gca().

* Update what's new for v0.21.0 and use rc_context() to temporarily change rcParams.

* Move bug fix from 0.21.0 whatsnew to 0.20.2.

* Allow passing in an rc to _gca() instead of just figsize, and added a test for boxplot figsize.

* Fix style violations.

(cherry picked from commit 044feb5)
TomAugspurger pushed a commit that referenced this pull request May 30, 2017
* Propagate the figsize via the rcParams, since matplotlib doesn't allow passing it as a parameter to gca().

* Update what's new for v0.21.0 and use rc_context() to temporarily change rcParams.

* Move bug fix from 0.21.0 whatsnew to 0.20.2.

* Allow passing in an rc to _gca() instead of just figsize, and added a test for boxplot figsize.

* Fix style violations.

(cherry picked from commit 044feb5)
stangirala pushed a commit to stangirala/pandas that referenced this pull request Jun 11, 2017
* Propagate the figsize via the rcParams, since matplotlib doesn't allow passing it as a parameter to gca().

* Update what's new for v0.21.0 and use rc_context() to temporarily change rcParams.

* Move bug fix from 0.21.0 whatsnew to 0.20.2.

* Allow passing in an rc to _gca() instead of just figsize, and added a test for boxplot figsize.

* Fix style violations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants